package com.example.androidtvdemo.demo03;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidtvdemo.R;

import java.util.ArrayList;
import java.util.List;

public class Demo03Activity extends FragmentActivity {

    private RecyclerView rv01, rv02;
    private TextAdapter textAdapter2;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo03);
        rv01 = findViewById(R.id.rv01);
        rv02 = findViewById(R.id.rv02);

        list = new ArrayList<>();
        list.add("小米");
        list.add("华为");
        list.add("苹果");
        list.add("香蕉");
        list.add("橙子");
        list.add("西瓜");
        list.add("小名");
        list.add("小答");
        list.add("哈哈");
        list.add("哈哈撒旦法");
        list.add("哈兰德");
        list.add("end");


        List<String> list2 = new ArrayList<>();
        list2.add("苹果");
        list2.add("香蕉");
        list2.add("橙子");
        list2.add("西瓜");
        list2.add("茄子");
        list2.add("小答");
        list2.add("哈哈");
        list2.add("哈哈撒旦法");
        list2.add("哈兰德");
        list2.add("end");


        List01Adapter textAdapter = new List01Adapter();
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv01.setLayoutManager(manager);
        rv01.setAdapter(textAdapter);
        //监听最后一个item的按键,切换到下一个rv的第一个元素
        textAdapter.setOnListener(new ItemListener() {
            @Override
            public void onHasFocus(View v, int position, boolean hasFocus) {
                if (hasFocus) {
                    handlerKeyListener(v, position);
                }
            }
        });
        textAdapter.setNewInstance(list);

        textAdapter2 = new TextAdapter();
        LinearLayoutManager manager2 = new LinearLayoutManager(this);
        manager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv02.setLayoutManager(manager2);
        rv02.setAdapter(textAdapter2);
        textAdapter2.setNewInstance(list2);
    }

    private void handlerKeyListener(View v, final int position) {
        v.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                        if (event.getAction() == KeyEvent.ACTION_DOWN) {
                            //必须滑动出来,让view可见
                            rv02.smoothScrollToPosition(0);
                            //rv02.scrollToPosition(0);
                            //其中2种方法,可能有时机问题,比如需要滚动rv才能看到目标,这个地方空指针了
                            //rv02.getLayoutManager().findViewByPosition(0).requestFocus();
                            //rv02.getChildAt(0).requestFocus();
                            //其中一种方法,需要聚焦的item的position
                            textAdapter2.setPosition(0);
                            textAdapter2.notifyItemChanged(0);
                        }
                        break;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        //右键也可以处理
                        // if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        //     if (position == list.size() - 1) {
                        //         rv02.smoothScrollToPosition(0);
                        //         textAdapter2.setPosition(0);
                        //         textAdapter2.notifyDataSetChanged();
                        //     }
                        // }
                        break;
                }
                return false;
            }
        });
    }
}